package com.ahucoding.rocket.mcpserver.service;

import com.ahucoding.rocket.client.SysUserClient;
import com.ahucoding.rocket.client.dto.SysUser;
import com.ahucoding.rocket.common.entity.ApiResponseBody;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {

    @Autowired
    private final SysUserClient sysUserClient;

    public record UserInfo(String id, String username, String name, String email, String mobile, 
                          String deptName, Integer status, String avatar) {
        public static UserInfo fromSysUser(SysUser sysUser) {
            if (sysUser == null) {
                return null;
            }
            return new UserInfo(
                    sysUser.getId(),
                    sysUser.getUsername(),
                    sysUser.getName(),
                    sysUser.getEmail(),
                    sysUser.getMobile(),
                    sysUser.getDeptName(),
                    sysUser.getStatus(),
                    sysUser.getAvatar()
            );
        }
    }

    @Tool(description = "本周a类施工情况")
    public UserInfo getUserInfo(String portalId, String userName) {
        log.info("开始调用sys-user服务获取用户信息，portalId: {}, userName: {}", portalId, userName);
        try {
            ApiResponseBody<SysUser> response = sysUserClient.getUserInfo(portalId, userName);
            log.info("调用sys-user服务获取用户信息完成");
            
            if (response != null && response.getStatus()) {
                return UserInfo.fromSysUser(response.getData());
            } else {
                log.error("调用sys-user服务获取用户信息失败: {}", response != null ? response.getMessage() : "响应为空");
                return null;
            }
        } catch (Exception e) {
            log.error("调用sys-user服务获取用户信息异常", e);
            return null;
        }
    }
    
    @Tool(description = "本日未闭环维修")
    public UserInfo findUserByUsername(String userName) {
        return getUserInfo("default", userName);
    }
}
